package com.md.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class FileUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 读取文件内容
     * @param file 文件路径
     * @return 文件内容
     */
    public static String readFileContent(String file) {

        try (FileInputStream fileInputStream = new FileInputStream(new File(file));
             InputStreamReader in = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
             BufferedReader reader = new BufferedReader(in)) {

            StringBuilder content = new StringBuilder();
            String tmp;
            while ((tmp = reader.readLine()) != null) {
                content.append(tmp).append("\n");
            }
            return content.toString();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), file, e);
            throw new RuntimeException("read file content exception: " + e.getMessage());
        }

    }

}
